package es.udc.fic.hamNeggs.web;

import org.apache.log4j.Logger;

import es.udc.fic.hamNeggs.dao.InputProject;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.pojo.Project;
import es.udc.fic.hamNeggs.pojo.Works;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet implementation class WelcomePage
 */
public class WelcomePage extends HttpServlet {
    private static final long   serialVersionUID = 1L;
    private final static Logger LOG              = Logger.getLogger(WelcomePage.class.getName());

    /**
     * @see HttpServlet#HttpServlet()
     */
    public WelcomePage() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession(true);

        final Long myUser = (Long) session.getAttribute("user");

        UserManager usrmng = new UserManager();

        // projects chart
        ArrayList<InputProject> projects = new ArrayList<InputProject>();

        final List<Works> userWorks = usrmng.getUser(myUser).getWorks();

        for (Works work : userWorks) {
            Project project = work.getProject();
            InputProject inputProject = new InputProject();
            inputProject.setProject(project);
            inputProject.setCurrentRol(work.getRol().getString());
            projects.add(inputProject);
        }

        request.setAttribute("projects", projects);

        request.setAttribute("user", usrmng.getUser(myUser).getFullName());

        usrmng.close();

        request.getRequestDispatcher("/welcomePage.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
